package com.yc.txx;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.yc.txx.controller.LoginInterceptor;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.yc.txx")
@EnableWebMvc
@MapperScan("com.yc.txx.mapper")
@EnableTransactionManagement // TODO 添加事务管理器
public class MvcConfig implements WebMvcConfigurer {

//    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }

//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        registry.addConverter(new TimestampConvertor());
//    }

//    @Autowired
//    LoginInterceptor interceptor;

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(interceptor)
//                .addPathPatterns("/a.html","/a.jsp","/showme");
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("file:D:\\代码\\毕设2025\\demo-txx\\src\\main\\webapp/");
                //.addResourceLocations("file:/root/txx/yc136/demo-txx/src/main/webapp/");
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:D:\\yc三期项目\\新教学视频\\5.14\\upload/");
                //.addResourceLocations("file:/root/txx/upload");
    }

    @Bean
    StandardServletMultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/txx?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("315685min");
        //dataSource.setPassword("aaaa");
        return dataSource;
    }

    @Bean("transactionManager")
    // 事务管理器
    public DataSourceTransactionManager dataSourceTransactionManager(
            DataSource dataSource
    ){
        return new DataSourceTransactionManager(dataSource);
    }


    @Bean
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        /**
         * 加载 mybatis 配置 ( 可选 )
         * setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
         */
        // ClassPathResource classPathResource = new ClassPathResource("mybatis.xml");
        // sqlSessionFactoryBean.setConfigLocation(classPathResource);
        /**
         * MyBatis配置类 等效于 mybatis.xml
         */
        MybatisConfiguration configuration = new MybatisConfiguration();
        /**
         * 配置日志框架 log4j, 等效于
         * <settings>
         *     <setting name="logImpl" value="LOG4J"/>
         * </settings>
         */
        configuration.setLogImpl(Log4jImpl.class);
        /**
         * MyBatis-Plus 分页插件
         */
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDialect(new MySqlDialect());
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        configuration.addInterceptor(mybatisPlusInterceptor);
        /**
         *  setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
         */
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/addCar.html",
                        // 提交订单的 ajax请求
                        "txx/lease/commit");
    }
}
